package com.ruoyi.web.controller.app;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.app.domain.bo.AppImFriendBo;
import com.ruoyi.app.domain.vo.AppImFriendVo;
import com.ruoyi.app.service.IAppImFriendService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 好友
 *
 * @author ruoyi
 * @date 2025-11-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/imFriend")
public class AppImFriendController extends BaseController {

    private final IAppImFriendService iAppImFriendService;


    /**
     * 同意好友申请
     */
    @SaCheckPermission("system:imFriend:list")
    @GetMapping("/agree")
    public R<Void> agree(AppImFriendBo bo) {
        return iAppImFriendService.agree(bo);
    }

    /**
     * 查询好友列表
     */
    @SaCheckPermission("system:imFriend:list")
    @GetMapping("/list")
    public TableDataInfo<AppImFriendVo> list(AppImFriendBo bo, PageQuery pageQuery) {
        return iAppImFriendService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出好友列表
     */
    @SaCheckPermission("system:imFriend:export")
    @Log(title = "好友", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppImFriendBo bo, HttpServletResponse response) {
        List<AppImFriendVo> list = iAppImFriendService.queryList(bo);
        ExcelUtil.exportExcel(list, "好友", AppImFriendVo.class, response);
    }

    /**
     * 获取好友详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:imFriend:query")
    @GetMapping("/{id}")
    public R<AppImFriendVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAppImFriendService.queryById(id));
    }

    /**
     * 新增好友
     */
    @SaCheckPermission("system:imFriend:add")
    @Log(title = "好友", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppImFriendBo bo) {
        return toAjax(iAppImFriendService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改好友
     */
    @SaCheckPermission("system:imFriend:edit")
    @Log(title = "好友", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppImFriendBo bo) {
        return toAjax(iAppImFriendService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除好友
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:imFriend:remove")
    @Log(title = "好友", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppImFriendService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
